# Alex Gazmararian
# agazmararian@gmail.com

library(here)

# Suppress readr progress bars
options(readr.show_progress = FALSE)

source(here("R", "load_functions.R"))

library(tidyverse)
library(tidylog)
library(modelsummary)
library(patchwork)
library(lmtest)
library(sandwich)
library(fixest)
library(marginaleffects)
library(tinytable)

# Treatment variable specifications----
# Named list with descriptive labels for treatment variables
# This centralizes variable definitions for use across all analysis scripts
treat.list <- c(
  "renewable_energy" = "d_re_2y_q",      # Renewable energy proximity (2-year window)
  "manufacturing" = "d_mfg_open_2y_q"    # Manufacturing proximity (2-year window)
)

# Extract components for convenience in analysis scripts
treat.vars <- unname(treat.list)    # Variable names: c("d_re_2y_q", "d_mfg_open_2y_q")
treat.labels <- names(treat.list)   # Descriptive labels: c("renewable_energy", "manufacturing")

# Model specification
covar.list <- c(
  # Individual-level covariates
  "age", "female", "black", "asian", "otherrace", "hispanic", "college", "employ_bin", "income5", "pid3", "gw_idx",
  # Local variables
  "urate_z", "force_ln_z", "gdp_ln_z", "income_pc_z",  "highway", "college_acs_z", "poverty_acs_z", "foreign_acs_z",
   "housing_acs_z", "popd_z", "bb100_bin", "demshare_z",
   # sample
   "sample"
)

# Load coefficient mapping for table presentation
source(here("R", "coefmap.R"))

# Standardized political party colors
party_colors <- list(
  Democrat = "#3B6BA5",
  Republican = "#B22222", 
  Independent = "#6AA84F",
  Neither = "#6AA84F"  # Using same color as Independent for "Neither" category
)

# Color scale functions for consistent usage
scale_color_party <- function() {
  scale_color_manual(values = c(
    "Democrat" = party_colors$Democrat,
    "Republican" = party_colors$Republican,
    "Independent" = party_colors$Independent,
    "Neither" = party_colors$Neither
  ))
}

scale_fill_party <- function() {
  scale_fill_manual(values = c(
    "Democrat" = party_colors$Democrat,
    "Republican" = party_colors$Republican,
    "Independent" = party_colors$Independent,
    "Neither" = party_colors$Neither
  ))
}

# Standardized colors for green project visibility
greenproj_colors <- list(
  "Not Visible" = "#377EB8",  # Blue
  "Visible" = "#E6AB02"       # Gold
)

# Color scale function for green project visibility
scale_fill_greenproj <- function() {
  scale_fill_manual(values = c(
    "Not Visible" = greenproj_colors$`Not Visible`,
    "Visible" = greenproj_colors$Visible
  ))
}

# Standardized text size for percentage labels in plots
plot_text_size <- 2.25
